package com.rbs.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbs.entity.Auction;
import com.rbs.service.IAuctionService;
import com.rbs.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rbs
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/auction")
public class AuctionController {
    @Resource
    private IAuctionService iAuctionService;
    @GetMapping("/selectAll")
    public List<Auction> selectAll(){
        return iAuctionService.list();
    }
    @GetMapping("/selectByPage")
    public IPage<Auction> selectByPage(Page page,Auction auction){
        //把auction里面的搜索条件转换为Wrapper
        QueryWrapper<Auction> wrapper = new QueryWrapper<>();
        if (auction != null){
            if (auction.getAuctionName()!=null&&!"".equals(auction.getAuctionName())){
                wrapper.eq("auctionName",auction.getAuctionName());
            }
            if (auction.getAuctionDesc()!=null&&!"".equals(auction.getAuctionDesc())){
                wrapper.like("auctionDesc",auction.getAuctionDesc());
            }
            if (auction.getAuctionStartTime()!=null){
                wrapper.eq("auctionStartTime",auction.getAuctionStartTime());
            }
            if (auction.getAuctionStartPrice()!=null){
                wrapper.eq("auctionStartPrice",auction.getAuctionStartPrice());
            }
        }

        return iAuctionService.page(page,wrapper);//进行分页
    }
    @DeleteMapping("/admin/delete/{id}")
    public void deleteById(@PathVariable("id") int id,@RequestHeader("Authorization") String token){
        DecodedJWT verify = JwtUtil.verify(token);
        String role = verify.getClaim("role").asString();
        if (role.equals("1")){
            iAuctionService.removeById(id);
        }else {
            throw new RuntimeException("没有权限");
        }
    }
}

